<?php 
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('foreground', 'member');
pc_base::load_sys_class('form', '', 0);

class index extends foreground  {
	private $course_db;
	private $classes;
	private $courses;
	private $course_time;
	private $weekdays = array('星期天','星期一','星期二','星期三','星期四','星期五','星期六');
	
	function __construct() {
		parent::__construct();
		$this->course_db = pc_base::load_model('courses_model');
		
		$this->_username = param::get_cookie('_username');
		$this->_userid = param::get_cookie('_userid');
		$this->_groupid = get_memberinfo($this->_userid,'groupid');
		$this->_modelid = get_memberinfo($this->_userid,'modelid');
		pc_base::load_app_func('global');
	}
	
	public function init() {
		
	}
	
	/**
	 * 展示课程安排
	 */
	public function show() {
		$where = '1';
		$return = array();
		$siteid = $_GET['siteid'] ? intval($_GET['siteid']) : get_siteid();
		if ($siteid) $where .= " AND `siteid`='".$siteid."'";
		$this->courses = $this->get_available_courses($siteid);
		$this->classes = $this->get_available_classes($siteid);
		$_GET['csid'] = intval($_GET['csid']);
		$_GET['cid'] = intval($_GET['cid']);
		$_GET['starttime'] = trim(strip_tags($_GET['starttime']));
		$_GET['endtime'] = trim(strip_tags($_GET['endtime']));
		
		if ($this->courses && $this->classes)
		{
			$page = max(intval($_GET['page']), 1);
			$available_classes_str = implode(',', array_keys($this->classes));
			$available_courses_str = implode(',', array_keys($this->courses));
			$where .= " AND `csid` IN ($available_courses_str) ";
			$where .= " AND `cid` IN ($available_classes_str) ";
			$where .= ' AND `ctused`=\'1\'';
			if ($_GET['csid']>0) $where .= " AND `csid`='{$_GET['csid']}' ";
			if ($_GET['cid']>0) $where .= " AND `cid`='{$_GET['cid']}' ";
			$stime = strtotime($_GET['starttime']);
			$where .= $stime>0 ? " AND `ctime`>='$stime' " : " AND `ctime`>='".SYS_TIME."' ";
			$etime = strtotime($_GET['endtime']);
			if ($etime>0 && $etime>$stime) $where .= " AND `ctime`<='$etime' ";

			$data = $this->course_db->listinfo($where, '`ctime` DESC, `ctid` DESC', $page);
			if ($data)
			{
				foreach ($data as $key=>$val)
				{
					$val['csname'] = $this->courses[$val['csid']];
					$val['cname'] = $this->classes[$val['cid']];
					$val['format_ctime'] = date("Y-m-d H:i:s", $val['ctime']);
					$val['format_week'] = $this->weekdays[date("w", $val['ctime'])];
					$return[$key] = $val;
				}
			}
		}
		$SEO = seo(get_siteid(), '提供最新的课程安排', "课程安排");
		include template('courses', 'show');
	}
	/**
	 * 删除绑定的课程
	 */
	function cdel()
	{
		$id = intval($_GET['id']);
		if ($id<1) showmessage("参数无效");	
		if ($this->_modelid==10)
		{
			$this->course_db->set_model(4);
			$field_key = 'student_id';
			$pra_id = 'sid';
		}
		elseif ($this->_modelid==11)
		{
			$this->course_db->set_model(3);
			$field_key = 'teacher_id';
			$pra_id = 'ctid';
		}
		//权限检查
		$where = " {$pra_id}='{$id}' AND {$field_key}='{$this->_userid}' AND is_allow=0 ";
		$this->course_db->delete($where);
		if ($this->course_db->affected_rows())
		{
			showmessage("操作成功", "index.php?m=courses&c=index&a=clist");
		}
		else
		{
			showmessage("操作失败，可能是记录不存在或者不属于你所有");
		}
	}
	
	/**
	 * 编辑课程
	 */
	function cedit()
	{
		$id = intval($_REQUEST['id']);
		if ($id<1) showmessage("参数无效");		
		$siteid = $_GET['siteid'] ? intval($_GET['siteid']) : get_siteid();
		$this->courses = $this->get_available_courses($siteid);
		$this->classes = $this->get_available_classes($siteid);
		$this->course_time =$this->get_available_course_time($siteid);	//可用的课时列表
		if ($this->_modelid==10)
		{
			$this->course_db->set_model(4);
			$field_key = 'student_id';
			$pra_id = 'sid';
		}
		elseif ($this->_modelid==11)
		{
			$this->course_db->set_model(3);
			$field_key = 'teacher_id';
			$pra_id = 'ctid';
		}		
		//权限检查
		$where = " {$pra_id}='{$id}' AND {$field_key}='{$this->_userid}' ";
		$record = $this->course_db->select($where);
		if (count($record)<1) showmessage("记录不存在或者不属于你");
		$record = $record['0'];
		if ($record['is_allow']) showmessage("抱歉，已通过绑定审核，无法编辑");
		//提交的数据
		/*if(isset($_POST['dosubmit'])) {
			$csid = intval($_POST['csid']);
			$cid = intval($_POST['cid']);
			if ($csid<1 || $cid<1) showmessage("参数不正确");
			//检查是否已经存在
			$where = " {$field_key} ='{$this->_userid}' AND class_id='$cid' AND course_id='$csid' AND {$pra_id}<>'$id' ";
			$exist = $this->course_db->get_one($where);
			if ($exist) showmessage("记录已存在");
			$insert_arr = array(
				'class_id' => $cid,
				'course_id' => $csid
			);
			$this->course_db->update($insert_arr, "{$pra_id}='$id'");
			if ($this->course_db->affected_rows())
			{
				showmessage("编辑成功", "index.php?m=courses&c=index&a=clist");
			}
			else 
			{
				showmessage("没有可更新的内容", "index.php?m=courses&c=index&a=clist");
			}
		}*/
		if(isset($_POST['dosubmit'])) {
			$ctid = intval($_POST['ctid']);
			if ($ctid<1) showmessage("参数不正确");
			//检查是否已经存在
			$where = " {$field_key} ='{$this->_userid}' AND course_time_id='$ctid' AND {$pra_id}<>'$id' ";
			$exist = $this->course_db->get_one($where);
			if ($exist) showmessage("记录已存在");
			//获得选中条目的相关信息
			$course_time_info = $this->get_special_course_time($ctid);
			if (!$course_time_info) showmessage("抱歉，您选择的课程不存在");
			//重设模型
			if ($this->_modelid==10)
			{
				$this->course_db->set_model(4);
			}
			elseif ($this->_modelid==11)
			{
				$this->course_db->set_model(3);
			}
			$insert_arr = array(
				'course_time_id' => $ctid,
				'class_id' => $course_time_info['cid'],
				'course_id' => $course_time_info['csid'],
				'add_time' => SYS_TIME,
			);
			$this->course_db->update($insert_arr, "{$pra_id}='$id'");
			if ($this->course_db->affected_rows())
			{
				showmessage("编辑成功", "index.php?m=courses&c=index&a=clist");
			}
			else 
			{
				showmessage("没有可更新的内容", "index.php?m=courses&c=index&a=clist");
			}
		}
		$record['main_id'] = $record[$pra_id];
		$SEO = seo(get_siteid(), '提供最新的课程安排', "课程安排");
		include template('courses', 'cedit');
	}
	
	/**
	 * 课程绑定功能
	 */
	public function clist()
	{//10 学生  11 教师  12 家长
		if (!@in_array($this->_modelid, array(10, 11))) showmessage(L('member_moudle_deny'));
		
		$siteid = $_GET['siteid'] ? intval($_GET['siteid']) : get_siteid();
		$this->courses = $this->get_available_courses($siteid);			//可用的课程列表
		$this->classes = $this->get_available_classes($siteid);			//可用的班级列表
		$this->course_time =$this->get_available_course_time($siteid);	//可用的课时列表
		$where = " 1 ";
		if ($this->_modelid==10)
		{
			$this->course_db->set_model(4);
			$where .= " AND student_id='{$this->_userid}' ";
			$field_key = 'student_id';
			$pra_id = 'sid';
		}
		elseif ($this->_modelid==11)
		{
			$this->course_db->set_model(3);
			$where .= " AND teacher_id='{$this->_userid}' ";
			$field_key = 'teacher_id';
			$pra_id = 'ctid';
		}
		//提交的数据
		/*if(isset($_POST['dosubmit'])) {
			$csid = intval($_POST['csid']);
			$cid = intval($_POST['cid']);
			if ($csid<1 || $cid<1) showmessage("参数不正确");
			//检查是否已经存在
			$where .= " AND class_id='$cid' AND course_id='$csid' ";
			$exist = $this->course_db->get_one($where);
			if ($exist) showmessage("记录已存在");
			$insert_arr = array(
				$field_key => $this->_userid,
				'class_id' => $cid,
				'course_id' => $csid,
				'add_time' => SYS_TIME,
			);
			$this->course_db->insert($insert_arr);
			if ($this->course_db->insert_id())
			{
				showmessage("添加成功", "index.php?m=courses&c=index&a=clist");
			}
		}*/
		if(isset($_POST['dosubmit'])) {
			$ctid = intval($_POST['ctid']);
			if ($ctid<1) showmessage("参数不正确");
			//检查是否已经存在
			$where .= " AND course_time_id='$ctid' ";
			$exist = $this->course_db->get_one($where);
			if ($exist) showmessage("记录已存在");
			//获得选中条目的相关信息
			$course_time_info = $this->get_special_course_time($ctid);
			if (!$course_time_info) showmessage("抱歉，您选择的课程不存在");
			//重设模型
			if ($this->_modelid==10)
			{
				$this->course_db->set_model(4);
			}
			elseif ($this->_modelid==11)
			{
				$this->course_db->set_model(3);
			}
			$insert_arr = array(
				$field_key => $this->_userid,
				'course_time_id' => $ctid,
				'class_id' => $course_time_info['cid'],
				'course_id' => $course_time_info['csid'],
				'add_time' => SYS_TIME,
			);
			$this->course_db->insert($insert_arr);
			if ($this->course_db->insert_id())
			{
				showmessage("添加成功，请等待管理员审核", "index.php?m=courses&c=index&a=clist");
			}
		}
		$page = max(intval($_GET['page']), 1);
		$course_list = $this->course_db->listinfo($where, '`add_time` DESC', $page);
		if ($course_list)
		{
			foreach ($course_list as $ckey=>$cval)
			{
				$course_list[$ckey]['main_id'] = $cval[$pra_id];
				//获得指定的课程ID
				$course_time_infos = $this->get_special_course_time($cval['course_time_id']);
				if ($course_time_infos)
				{
					$course_list[$ckey]['course_id'] = $this->courses[$course_time_infos['csid']];
					$course_list[$ckey]['class_id'] = $this->classes[$course_time_infos['cid']];
					$course_list[$ckey]['course_time'] = $course_time_infos['ctime'];
					$course_list[$ckey]['course_note'] = $course_time_infos['cnote'];
				}
				else 
				{
					$course_list[$ckey]['course_id'] = $this->courses[$cval['course_id']];
					$course_list[$ckey]['class_id'] = $this->classes[$cval['class_id']];
					$course_list[$ckey]['course_time'] = 0;
					$course_list[$ckey]['course_note'] = '';
				}
			}
		}
		$SEO = seo(get_siteid(), '提供最新的课程安排', "课程安排");
		include template('courses', 'clist');
	}
	
	/**
	 * 获得可用的课程
	 */
	private function get_available_courses($siteid, $all = false)//$all 为真时，忽略是否可用的条件
	{
		$return = array();
		$this->course_db->set_model(2);
		$sql_param = $all ? array('siteid'=>$siteid) : array('siteid'=>$siteid, 'csused'=>"1") ;
		$query = $this->course_db->select($sql_param);
		if ($query)
		{
			foreach ($query as $key=>$val)
			{
				$return[$val['csid']] = $val['csname'];
			}
		}
		$this->course_db->set_model();
		return $return;
	}	

	/**
	 * 获得可用的班级列表
	 */
	private function get_available_classes($siteid, $all = false)
	{
		$return = array();
		$this->course_db->set_model(1);
		$sql_param = $all ? array('siteid'=>$siteid) : array('siteid'=>$siteid, 'cused'=>"1") ;
		$query = $this->course_db->select($sql_param);
		if ($query)
		{
			foreach ($query as $key=>$val)
			{
				$return[$val['cid']] = $val['cname'];
			}
		}
		$this->course_db->set_model();
		return $return;
	}
	
	/**
	 * 获得可用的课时列表清单,大于当前时间3个小时的课程列为可用课程
	 */
	private function get_available_course_time($siteid)
	{
		$return = array();
		$this->course_db->set_model(0);
		//$nowTime = SYS_TIME + 3*3600;
		$where = " siteid='$siteid' AND ctused='1' ";// AND ctime>'$nowTime' ";
		$query = $this->course_db->select($where);
		if ($query)
		{
			//获得可用的班级列表
			$classes = $this->get_available_classes($siteid);
			//获得可用的课程列表
			$courses = $this->get_available_courses($siteid);
			
			foreach ($query as $key=>$val)
			{
				//如果课程或者班级不在可用列表里面，不显示
				if (!@array_key_exists($val['csid'], $courses) || !@array_key_exists($val['cid'], $classes))
				{
					continue;
				}
				$return[$val['ctid']] = $classes[$val['cid']].'  '.$courses[$val['csid']];
			}
		}
		$this->course_db->set_model();
		return $return;
	}
	
	/**
	 * 获得指定课时的相关信息
	 */
	private function get_special_course_time($ctid)
	{
		$this->course_db->set_model(0);
		$return = $this->course_db->get_one(array('ctid' => $ctid));
		$this->course_db->set_model();
		return $return;
	}
}
?>